//
//  ReadViewController.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "ReadViewController.h"
@interface ReadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, CarouselPictureDelegate>
@property (nonatomic, retain)NSMutableArray *carouseArr;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain)UICollectionView *collectionView;


@end

@implementation ReadViewController
- (void)dealloc
{
    [_carouseArr release];
    [_dataArr release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"阅读";
    //数据处理
    [self dataHandle];
    //创建layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake((Width - 10) / 3, (HEIGHT - 150 - 49 - 20) / 3);
    //创建collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 155, Width - 10, 667 - 49 - 155) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [layout release];
    [_collectionView release];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.collectionView registerClass:[ReadCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

}
//点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReadDetailViewController *detailVC = [[ReadDetailViewController alloc] init];
    Model *m = _dataArr[indexPath.item];
    detailVC.tid = m.type.integerValue;
    detailVC.title1 = m.name;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
    
}
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
//cell赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    Model *m = _dataArr[indexPath.item];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
    cell.nameLabel.text = [m.name stringByAppendingString:[NSString stringWithFormat:@" %@", m.enname]];
    return cell;
}
//数据处理
- (void)dataHandle


{
    NSString *urlStr = @"http://api2.pianke.me/read/columns";
    NSDictionary *dic = @{@"client": @2};
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        NSArray *arr = result[@"data"][@"carousel"];
        self.carouseArr = [Model modelHanderWithArray:arr];
        [self creatCarouse];
        NSMutableArray *arr1 = result[@"data"][@"list"];
        self.dataArr = [Model modelHanderWithArray:arr1];
        [_collectionView reloadData];
    }];
    
}
//轮播图
- (void)creatCarouse
{
    NSMutableArray *mArr = [NSMutableArray array];
    NSMutableArray *urlArr = [NSMutableArray array];
    for (Model *m in self.carouseArr) {
        [mArr addObject:m.img];
        [urlArr addObject:m.url];
    }
    CarousePhotoView *carouseView = [[CarousePhotoView alloc] initWithFrame:CGRectMake(5, 0, Width - 10, 150) timeInterval:5 imageUrlArr:mArr urlArr:urlArr];
    carouseView.delegate = self;
    [self.view addSubview:carouseView];
    [carouseView release];
}
- (void)pushWithUrl:(NSString *)url
{
    NSString *str = [url substringFromIndex:17];
    ReadWebVC *webVC = [[ReadWebVC alloc] init];
    webVC.contenId = str;
    [self.navigationController pushViewController:webVC animated:YES];
    [webVC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
